/*
 *  Copyright 2010 dmakariev.
 * 
 */
package org.deltaset.faces.upload;

import java.util.Map;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import org.deltaset.faces.FacesUtils;

/**
 *
 * @author dmakariev
 */
//@FacesComponent(value = "compositeFileUpload")
public class UploadNamingContainer extends UINamingContainer {

    public UploadNamingContainer() {
    }

    @Override
    public String getFamily() {
        return "javax.faces.NamingContainer";
    }

    @Override
    public void decode(final FacesContext context) {
        
        final String clientId = this.getClientId(context);

        final Map<String, Object> attrs = this.getAttributes();
        final Object o = attrs.get("value");
        final ValueExpression valueVE = this.getValueExpression("value");
        final String veString = valueVE.getExpressionString();


        if (false == FileRequestHolder.isEmpty()) {
            final CurrentFile currentFile = FileRequestHolder.getFileParameter(clientId);

            final ELContext currentELContext = FacesContext.getCurrentInstance().getELContext();
            final Object fileHolder = valueVE.getValue(currentELContext);

            if (fileHolder instanceof CurrentFileAware) {
                ((CurrentFileAware) fileHolder).setCurrentFile(currentFile);
            } else {
                FacesUtils.setValueExpression(veString, currentFile);
            }          
        }
        super.decode(context);
    }    
}
